/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modele;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;


public class DAO {
    
    
    public static Collection<Dep> getLesDeps() {
        Collection<Dep> departement = new TreeSet<Dep>();
        try {
            Statement req;
            Connection con = Connect.get();          
            req = con.createStatement();    
            ResultSet rs;
            rs = req.executeQuery("Select DISTINCT departement from medecin order by departement ASC");
            //Parcours
            while (rs.next()) {
                String numDep = rs.getString(1);
//                Collection<Med> colMedecin = new TreeSet<Med>();
//                Statement req2;
//                req2 = con.createStatement();
//                ResultSet rs2 = req2.executeQuery("select id, nom, prenom, adresse, tel, specialitecomplementaire"
//                        + " from medecin where departement='" + numDep + "'");
                departement.add(new Dep(numDep));                
//                rs2.close();
            }
            rs.close();
            req.close();
        } catch (SQLException ex) {
            Logger.getLogger(Pays.class.getName()).log(Level.SEVERE, null, ex);
        }
        return departement;
    }

    static Collection<Med> getLesMeds() {
        Collection<Med> medecin = new TreeSet<Med>();
        try {
            Statement req;
            Connection con = Connect.get();          
            req = con.createStatement();    
            ResultSet rs;
            rs = req.executeQuery("Select * from medecin");
            //Parcours
            while (rs.next()) {
                String id = rs.getString("id");
                String nom = rs.getString("nom");
                String prenom = rs.getString("prenom");
                String adresse = rs.getString("adresse");
                String tel = rs.getString("tel");
                String spe = rs.getString("specialitecomplementaire");
                String dep = rs.getString("departement");
                
//                Collection<Med> colMedecin = new TreeSet<Med>();
//                Statement req2;
//                req2 = con.createStatement();
//                ResultSet rs2 = req2.executeQuery("select id, nom, prenom, adresse, tel, specialitecomplementaire"
//                        + " from medecin where departement='" + numDep + "'");
                medecin.add(new Med(nom, prenom, adresse, tel, spe, id, dep));                
//                rs2.close();
            }
            rs.close();
            req.close();
        } catch (SQLException ex) {
            Logger.getLogger(Pays.class.getName()).log(Level.SEVERE, null, ex);
        }
        return medecin;
    }

    static Collection<Spe> getLesSpes() {
        Collection<Spe> specialite = new TreeSet<Spe>();
        try {
            Statement req;
            Connection con = Connect.get();          
            req = con.createStatement();    
            ResultSet rs;
            rs = req.executeQuery("Select DISTINCT specialitecomplementaire from medecin where specialitecomplementaire IS NOT NULL");
            //Parcours
            while (rs.next()) {
                String nomSpe = rs.getString(1);
//                Collection<Med> colMedecin = new TreeSet<Med>();
//                Statement req2;
//                req2 = con.createStatement();
//                ResultSet rs2 = req2.executeQuery("select id, nom, prenom, adresse, tel, specialitecomplementaire"
//                        + " from medecin where departement='" + numDep + "'");
                specialite.add(new Spe(nomSpe));                
//                rs2.close();
            }
            rs.close();
            req.close();
        } catch (SQLException ex) {
            Logger.getLogger(Pays.class.getName()).log(Level.SEVERE, null, ex);
        }
        return specialite;
    }
}   